//index.js
const utils = require("../../utils/utils.js");
const biz = require("../../utils/bizUtils.js");
const listUtil = require("../../utils/listUtil.js");
const session = require("../../utils/session.js");
const toast = require("../../utils/toasts.js");
const wm = require("../../utils/wechat_modules.js");
const com = require("../../utils/commonRequest.js");
const cache = require("../../utils/cache.js");

import {
  cus_change_status,
  cus_tags,
  agent_verify
} from "../../utils/api.js";

//获取应用实例
const app = getApp()
const globalData = app.globalData;

Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    list: [],
    hidden: true,
    scrollTop: 0,
    scrollHeight: 0,
    title: '',
    keyword: null,
    catId: null,
    orderRule: 'HOT',
    waiting: true,
    share_modal: false,
    noMore: false,
    banners: ['https://img.lingxing.site/jx/small_banner1_1.png'],
    isIphoneX: globalData.isIphoneX,
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad: function(options) {
    var that = this;
    toast.showLoading();
    wx.getSystemInfo({
      success: function(res) {
        console.info(res.windowHeight);
        that.setData({
          scrollHeight: res.windowHeight
        });
      }
    });

    if (options) {
      if (options.sellerId) {
        this.setData({
          sellerId: options.sellerId
        });
      }

      if (options.from) {
        this.setData({
          from: options.from
        })
      }


      if (options.salesId) {
        this.setData({
          salesId: options.salesId
        })
        if (options.salesName) {}
      }

      if (options.index) {
        this.setData({
          index: options.index
        });
      }

      if (options.type) {
        this.setData({
          type: options.type,
        })
      }
    }
    this.getUserInfo();
    // this.getMyCard();
    this.getList();
  },
  onShow: function() {},
  onShareAppMessage: function(res) {
    if (this.data.isAgent && this.data.agentIndex != null) {
      var list = this.data.list;
      var obj = list[this.data.agentIndex];
      var url = '/pages/agentAdd/agentAdd?topId=' + obj.topSellerId + '&upId=' + obj.sellerId;

      return wm.baseShare(this, '欢迎代理' + obj.company + '的商品', url);
    }

    return wm.commonShare(this);
  },
  getList() {
    console.log("---" + this.data.location);
    var sellerId = this.data.sellerId;

    var queryParam = {
      sellerId
    };
    var api = "/seller/queryStaff";

    var that = this;

    listUtil.getList(this, api, queryParam, function(item) {
      // console.log(item);
      item.summary = '金牌教练·' + utils.calAge(item.driveTime) + '年驾龄·' + utils.calAge(item.teachTime) + '年教龄';
      item.rateScore = biz.rateScore(item.totalRateScore, item.totalRateCount);
      item.rates = biz.rates(item.rateScore);
      item.rateTip = item.rateScore + '分';
      if(!item.rateScore) {
        item.rateTip = '无评分';
      }
    });
  },
  bindDownLoad: function() {
    getList();
  },
  scroll: function(event) {
    listUtil.scroll(this, event);
  },
  refresh: function(event) {
    listUtil.refresh(this);
  },
  onPullDownRefresh: function() {
    listUtil.onPullDownRefresh(this);
  },
  onReachBottom: function() {
    listUtil.onReachBottom(this);
  },
  // 获取当前用户信息
  getUserInfo: function() {
    session.getUser(this);
  },
  pushFormSubmit: function(e) {
    console.log(e);
    console.log("formId:" + e.detail.formId);
    com.saveFormId(e);
  },
  select: function(e) {
    var index = e.currentTarget.dataset.index;
    var list = this.data.list;
    var item = list[index];
    cache.put('staffSelect', item);
    wx.navigateBack({});
  }
})